<?php
/**
 * Created by PhpStorm.
 * User: acker
 * Date: 21-06-08
 * Time: 下午6:25
 */

namespace App\Models\yb;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;
use Encore\Admin\Facades\Admin;



class GoodsWarehouseModel extends Model
{
    use DefaultDatetimeFormat;

    use SoftDeletes;

    /**
     *
     *计量单位
     * @var string
     */
    protected $table = 'yb_goods_warehouse';

    /**
     * 主键
     *
     * @var string
     */
    protected $primaryKey = 'id';



    /**
     * 该模型是否被自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 主键
     *
     * @var string
     */
    protected $appends = ['action_id'];

    /**
     * @return mixed
     */
    public function getActionIdAttribute(){
        return $this->id;
    }

    public function setGoodsBannerAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['goods_banner'] = json_encode($pictures);
        }
    }

    public function getGoodsBannerAttribute($pictures)
    {
        return json_decode($pictures, true);
    }

    /**
     * @return array
     */
    protected function selectOptions($where=array()){
        $data_array =array();

        if(empty($where)){
            $data = self::get();
        }else{
            $data = self::where($where)->get();
        }

        foreach($data as $k=>$v){
            $data_array[$v['id']] = $v['goods_title'];
        }
        return $data_array;
    }

    /**
     * @param $where_filed
     * @param $where_val
     * @param string $filed
     * @return string
     */
    protected function selectValue($where_filed,$where_val,$filed = 'goods_title'){
        $list = self::where($where_filed,$where_val)->select($filed)->first();
        if(!empty($list)){
            return $list[$filed];
        }else{
            return "";
        }
    }

}
